<?php

class Ctl_Services extends ControlBase {

    var $modul = "info";

    function Ctl_Services() {
// Constructor of father class.
	ControlBase::ControlBase();
    }

    function execute($action = "") {
	global $db;
	global $arrayError, $_POST;
	include_once DIR_MODEL . 'md_services.class.php';
	$objServices = new MD_Services($db);
	$arrayData = array();
// Get back the work you want to do.
	$this->modul = CM_getParameter(MODUL);
	if ($action == "") {
// Get back the action: as: Add, Delete, Edit or List.
	    $action = CM_getParameter(ACTION);
	}

	switch ($action) {
	    case "Edit_type":
		$this->do_edit_services_type();
		break;
	    case "add_services_type":
		$this->do_add_services_type();
		break;
	    case "Delete_type":
		$this->do_delete_type();
		break;
	    case "Edit_services":
		$this->do_edit_services();
		break;
	    case "add_services":
		$this->do_add_services();
		break;
	    case "Del_services":
		$this->do_delete_services();
		break;
	    case "services_type":
		$arrayData = $objServices->get_services_type_by_Where("", "", "");
		$arrayData_Service_type = "";
		include_once DIR_VIEW . 'v_service_type.php';
		V_Services_Type($arrayData, $arrayData_Service_type);
		break;
	    case "List":
	    default :
		$parent_id = CM_getParameter('parent_id');
		if ($parent_id != "") {
		    $listField = "*";
		    $where = "services_type='$parent_id'";
		    $order = " id DESC";
		    $arrayData = $objServices->getByWhere($listField, $where, $order);
		    $where1 = " id = '$parent_id'";
		    $order1 = "";
		    $limit1 = "";
		    $arrayData_Service_type = $objServices->get_services_type_by_Where_1($where1, $order1, $limit1);
		} else {
		    $arrayData = $objServices->getAll("*", " ORDER BY id DESC");
		    $arrayData_Service_type = "";
		}
		include_once DIR_VIEW . 'v_services_list.php';
		V_Services_List($arrayData, $arrayData_Service_type);
		break;
	}

	unset($arrayData);
    }

    function do_delete_type() {
	global $db;
	global $arrayError, $_POST;
	$arrayData = array();
	$objServices = new MD_Services($db);

	$id = CM_getParameter("id");
	if ($this->delete_type($id)) {
	    $ulr = "?" . MODUL . "=" . $this->modul . "&" . ACTION . "=services_type";
	    CM_gotoPage($ulr);
	} else {
	    $arrayError[] = " Có lỗi trong quá trình xóa dữ liệu.";
	}
    }

    function do_delete_services() {
	global $db;
	global $arrayError, $_POST;
	$arrayData = array();
	$objServices = new MD_Services($db);

	$id = CM_getParameter("id");
	if ($this->delete_services($id)) {
	    $ulr = "?" . MODUL . "=" . $this->modul . "&" . ACTION . "=services_list";
	    CM_gotoPage($ulr);
	} else {
	    $arrayError[] = " Có lỗi trong quá trình xóa dữ liệu.";
	}
    }

    function delete_services() {
	global $arrayError, $_POST;
	$objServices = new MD_Services($this->db);
	$id = CM_getParameter("id");
	$objServices->delete_services($id);
	if ($objServices->errno()) {
	    $arrayError[] = $objServices->error();
	    return false;
	}
	return true;
    }

    function delete_type() {
	global $arrayError, $_POST;
	$objServices = new MD_Services($this->db);
	$id = CM_getParameter("id");
	$objServices->delete_type($id);
	if ($objServices->errno()) {
	    $arrayError[] = $objServices->error();
	    return false;
	}
	return true;
    }

    function do_edit_services_type() {
	global $db;
	global $arrayError, $_POST;
	$arrayData = array();
	$objServices = new MD_Services($db);

	include_once DIR_VIEW . 'v_add_services_type.php';

	if (isset($_POST['Submit'])) {
	    if ($this->update_type()) {
		$ulr = "?" . MODUL . "=" . $this->modul . "&" . ACTION . "=services_type";
		CM_gotoPage($ulr);
	    } else {
		$arrayData = &$_POST;
		V_Add_Services_Type($arrayData, "Edit_type");
	    }
	} else {
	    $id = CM_getParameter("id");
	    $where1 = " id = '$id'";
	    $order1 = "";
	    $limit1 = "";
	    $arrayData = $objServices->get_services_type_by_Where_1($where1, $order1, $limit1);
	    V_Add_Services_Type($arrayData, "Edit_type");
	}
    }

    function do_edit_services() {
	global $db;
	global $arrayError, $_POST;
	$arrayData = array();
	$objServices = new MD_Services($db);

	include_once DIR_VIEW . 'v_add_services.php';

	if (isset($_POST['Submit'])) {
	    if ($this->update_services()) {
		$ulr = "?" . MODUL . "=" . $this->modul . "&" . ACTION . "=services_list";
		CM_gotoPage($ulr);
	    } else {
		$arrayData = &$_POST;
		V_Add_Services($arrayData, "Edit_services");
	    }
	} else {
	    $id = CM_getParameter("id");
	    $where1 = " id = '$id'";
	    $order1 = "";
	    $limit1 = "";
	    $arrayData = $objServices->getById2($id, "*");
	    V_Add_Services($arrayData, "Edit_services");
	}
    }

    function do_add_services_type() {
	global $db;
	global $arrayError, $_POST;
	$arrayData = array();
	$objServices = new MD_Services($db);

	include_once DIR_VIEW . 'v_add_services_type.php';

	if (isset($_POST['Submit'])) {
	    if ($this->insert_type()) {
		$ulr = "?" . MODUL . "=" . $this->modul . "&" . ACTION . "=services_type";
		CM_gotoPage($ulr);
	    } else {
		$arrayData = &$_POST;
		V_Add_Services_Type($arrayData, "add_services_type");
	    }
	} else {
	    V_Add_Services_Type($arrayData, "add_services_type");
	}
    }

    function do_add_services() {
	global $db;
	global $arrayError, $_POST;
	$arrayData = array();
	$objServices = new MD_Services($db);

	include_once DIR_VIEW . 'v_add_services.php';

	if (isset($_POST['Submit'])) {
	    if ($this->insert_services()) {
		$ulr = "?" . MODUL . "=" . $this->modul . "&" . ACTION . "=services_list";
		CM_gotoPage($ulr);
	    } else {
		$arrayData = &$_POST;
		V_Add_Services($arrayData, "add_services");
	    }
	} else {
	    V_Add_Services($arrayData, "add_services");
	}
    }

    function update_type() {
	global $arrayError, $_POST, $db;
	$objServices = new MD_Services($db);
	$objServices->update_type(&$_POST);
	if ($objServices->errno()) {
	    $arrayError[] = $objServices->error();
	    return false;
	}
	return true;
    }

    function update_services() {
	global $arrayError, $_POST, $db;
	$objServices = new MD_Services($db);
	$id = CM_getParameter("id");
		$objServices->update_services(&$_POST);
	if ($objServices->errno()) {
	    $arrayError[] = $objServices->error();
	    return false;
	}
	unset($_SESSION['Pkd_images1']);
	return true;
    }

    function insert_type() {
	global $arrayError, $_POST, $db;
	$objServices = new MD_Services($db);
	$objServices->insert_type(&$_POST);
	if ($objServices->errno()) {
	    $arrayError[] = $objServices->error();
	    return false;
	}
	return true;
    }

    function insert_services() {
	global $arrayError, $_POST, $db;
	$objServices = new MD_Services($db);
	$objServices->insert_services(&$_POST);
	if ($objServices->errno()) {
	    $arrayError[] = $objServices->error();
	    return false;
	}
	return true;
    }

}

?>